<?php
Prado::using('Application.common.common');
class Master extends TTemplateControl
{
	public function onInit($param)
	{
		parent::onInit($param);
		date_default_timezone_set('Asia/Singapore');
		if (!$this->Page->IsPostBack)
		{				
			if ($this->Application->User->UserName != "") 
			{
				$this->lblWelcome->Text = "Welcome ";
				$this->lnkProfile->Text = $this->Application->User->UserName;
				$this->lblWelcome->Visible = true;				
				$this->lnkProfile->Visible = true;				
				$this->lblLine1->Visible = true;
				$this->lblLine2->Visible = true;
				$this->lnkLogOut->Visible = true;	
				
				$this->hoverLeft1->Visible = false;	
				$this->hoverRight1->Visible = false;	
				$this->hoverLeft2->Visible = false;	
				$this->hoverRight2->Visible = false;	
				$this->lnkLogin->Visible = false;	
				$this->lnkRegister->Visible = false;
				
				if (Common::checkAdmin($this->Application->User))
					$this->lnkAdmin->Visible=true;
				else 
					$this->lnkPostFoodPlace->Visible = true;	
			}
			else
			{			
				$this->lblWelcome->Text = "Welcome Guest | ";
				$this->lblWelcome->Visible = true;		
				$this->lnkPostFoodPlace->Visible = true;
			}
			
			if ($this->Request->contains('m')) {
				if ($this->Request['m'] == 'cu')
					$this->lblMessage->Text = "User has been registered successfully. <img src='".$this->Request->UrlManagerModule->UrlPrefix."/themes/default/images/icon_hide.png' height='12px;' />";
				else if ($this->Request['m'] == 'cr')
					$this->lblMessage->Text = $this->Application->getModule('message')->translate('ADD_SUCCESS','Review','') . " <img src='".$this->Request->UrlManagerModule->UrlPrefix."/themes/default/images/icon_hide.png' height='12px;' />";
				else if ($this->Request['m'] == 'pfp')
					$this->lblMessage->Text = $this->Application->getModule('message')->translate('ADD_SUCCESS','Food Place','') . " <img src='".$this->Request->UrlManagerModule->UrlPrefix."/themes/default/images/icon_hide.png' height='12px;' />";
				else if ($this->Request['m'] == 'sns')
					$this->lblMessage->Text = $this->Application->getModule('message')->translate('ADD_SUCCESS','Your email','') . " <img src='".$this->Request->UrlManagerModule->UrlPrefix."/themes/default/images/icon_hide.png' height='12px;' />";			
				$this->lblMessage->Style = "color:blue;";					
				$this->lblMessage->Visible = true;
			}
		}		
	}
	
	protected function getUrlPrefix()
	{
		return $this->Request->UrlManagerModule->UrlPrefix;
	}	
	
	protected function btnLogout_Clicked($sender, $param)
	{
		$this->Application->getModule("auth")->logout();
		$this->Response->redirect($this->Service->ConstructUrl("Index"));
	}
	
	public function btnGoogleSearch_Clicked($sender, $param)
	{	
		$query = $this->txtGoogleSearch->SafeText;
		if ($query == "Google Search")
			$query = "";
		//$this->Response->redirect("http://www.google.com/search?q=" . $query);
		$this->Response->redirect($this->Service->ConstructUrl("Index"));
	}	
	
	public function btnNewsLetter_Clicked($sender, $param)
	{	
		$email = $this->txtSubcribeEmail->SafeText;
		$userRecord = UserRecord::finder()->findByUser_Email($email);
		if ($userRecord instanceof UserRecord)
		{
			$userRecord->ReceiveUpdates = "Y";
			$userRecord->save();
			$this->Response->redirect($this->Service->ConstructUrl("Index", array('m'=>'sns')));	
		}
		else
		{
			$newsletterRecord = new NewsletterRecord;
			$newsletterRecord->Email = $email;
			$newsletterRecord->Status = "1";
			$newsletterRecord->save();	
			$this->Response->redirect($this->Service->ConstructUrl("Index", array('m'=>'sns')));
		}
	}	
}
?>